package com.updateeproject.service.impl;
//2222 在service这里，不能把我们数据库的映射简简单单地（随随便便地）返回给想要Service的服务
//也就是说，我们在Service层，必须有一个Model的概念
//在service包里先创建一个model包，用来放model类 创建一个UserModel类  核心领域模型对象
//这个model才是真正意义上定义    SpringMVC中    业务逻辑交互的概念
//3-1 6\7分钟

import com.updateeproject.dao.UserDOMapper;
import com.updateeproject.dao.UserPasswordDOMapper;
import com.updateeproject.dataobject.UserDO;
import com.updateeproject.dataobject.UserPasswordDO;
import com.updateeproject.error.BusinessException;
import com.updateeproject.error.EmBusinessError;
import com.updateeproject.service.UserService;
import com.updateeproject.model.UserModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
/**
 * 弄个方法，得到一个Model的对象
 */
import javax.annotation.Resource;

@Service//用来指明这个UserServiceImpl就是Spring的Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDOMapper userDOMapper;
    @Resource
    private UserPasswordDOMapper userPasswordDOMapper;


    @Override
    public UserDO getUserById(Integer id) {
        UserDO userDO = userDOMapper.selectByPrimaryKey(id);

        if (userDO == null) {
            return null;
        }

        return userDO;
    }

    @Override
    public void register(UserModel userModel) throws BusinessException {
        if (userModel == null) {
            // TODO: 2019/6/10 那个没有返回值的话 不能return null；
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        if (StringUtils.isEmpty(userModel.getName())
                || userModel.getGender() == null
                || StringUtils.isEmpty(userModel.getTelphone())) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }


        //实现Model到DO的一个方法（model->dataobject）

        UserDO userDO = new UserDO();
        userDOMapper.insertSelective(userDO);


        UserPasswordDO userPasswordDO = new UserPasswordDO();
        userPasswordDOMapper.insertSelective(userPasswordDO);

        return;

    }

    private UserDO convertFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userModel, userDO);
        return userDO;

    }

    private UserPasswordDO convertPasswordFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserPasswordDO userPasswordDO = new UserPasswordDO();
        //  userPasswordDOMapper.insertSelective(userPasswordDO);
        userPasswordDO.setEncrptPassword(userModel.getEncrptPassword());
        userPasswordDO.setUserId(userModel.getId());
        return userPasswordDO;

    }

    //传进来的"UserDO"，"UserPasswordDO" 组装成"UserModel"
    private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
        /**
         * 别忘记判空处理问题
         * 3-1 8分钟
         */
        if (userDO == null) {
            return null;
        }

        UserModel userModel = new UserModel();
        //把UserDO的属性，copy到UserModel中，用下面这个BeanUtils.copyProperties(Object source ,Object target)
        //而且要确保字段名、类型一致，权限修饰符需要一样不？
        BeanUtils.copyProperties(userDO, userModel);

        if (userPasswordDO != null) {
            userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
        }
//        BeanUtils.copyProperties(userPasswordDO,userModel);

        return userModel;
    }
}
